package com.bdqn.service;

import com.bdqn.mapper.SysRoleMapper;
import com.bdqn.pojo.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色功能模块业务实现类
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 分页查询角色列表
     * @return
     */
    @Override
    public List<SysRole> selectList() {
        try {
            return sysRoleMapper.selectList();
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 查询总记录数
     * @return
     */
    @Override
    public int count() {
        try {
            return sysRoleMapper.count();
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }
    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @Override
    public boolean add(SysRole sysRole){
        boolean result = false;
        try{
            if(sysRoleMapper.add(sysRole)==1){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 根据ID获取角色名称
     *
     * @param roleId
     * @return
     */
    @Override
    public SysRole getId(Integer roleId){
        return sysRoleMapper.getId(roleId);
    }

    /**
     * 根据ID进行删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        boolean result = false;
        try{
            if(sysRoleMapper.delete(id)==1){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 修改角色
     *
     * @param sysRole
     * @return
     */
    @Override
    public boolean update(SysRole sysRole) {
        boolean result = false;
        try{
            if(sysRoleMapper.update(sysRole)==1){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 查询角色编码是否重复
     *
     * @param code
     * @return
     */
    @Override
    public boolean getCode(String code) {
        boolean result = false;
        try{
            if(sysRoleMapper.getCode(code) != null){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
